package com.nameless.filter;

import com.alibaba.fastjson.JSON;
import com.nameless.util.JwtUtil;
import com.nameless.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

/**
 * @author keluoyisi
 * @program: tong-parent
 * @description:
 * @date 2023-02-23 22:39:22
 */
@Component
@Slf4j
public class LoginFilter implements GlobalFilter, Ordered {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Value("${pass.whiteList}")
    private String[] whiteList;


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpResponse response = exchange.getResponse();
        ServerHttpRequest request = exchange.getRequest();
        String path = request.getPath().toString();
        log.debug("============================"+path);
        if ("/login".equals(path)){
           return chain.filter(exchange);
        }
        if ("/login1".equals(path)){
            return chain.filter(exchange);
        }
        // 放行获取白名单信息,名单在配置文件中
        for (String s : whiteList) {
            if(s.equals(path)){
                return chain.filter(exchange);
            }
        }
        // 放行获取白名单信息,名单在配置文件中
        for (String s : whiteList) {
            if(path.startsWith(s)){
                return chain.filter(exchange);
            }
        }

        // 放行获取白名单信息,名单在配置文件中
        for (String s : whiteList) {
            if(path.startsWith(s)){
                return chain.filter(exchange);
            }
        }

        String token = request.getHeaders().getFirst("token");

        System.out.println(token);
        if (StringUtils.hasText(token)&& JwtUtil.verifyToken(token)&&stringRedisTemplate.hasKey(token)){
            stringRedisTemplate.expire(token,1, TimeUnit.DAYS);
            return chain.filter(exchange);
        }
        Result<String> result=new Result<>(5001,"登录失效");
        //3.3作JSON转换
        byte[] bytes = JSON.toJSONString(result).getBytes(StandardCharsets.UTF_8);

        //3.4调用bufferFactory方法,生成DataBuffer对象
        DataBuffer buffer = response.bufferFactory().wrap(bytes);

        //4.调用Mono中的just方法,返回要写给前端的JSON数据
        return response.writeWith(Mono.just(buffer));
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
